﻿/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using VMuktiAPI;

namespace VMukti.Presentation.Controls
{
    public enum ImageType
    {
        Page,
        Module,
        MaleBuddy,
        FemaleBuddy
    }

    public partial class CtlExpanderContent : System.Windows.Controls.UserControl, IDisposable
    {
        public delegate void DelItemSelectionChanged(string strTagText, string strContent);
        public event DelItemSelectionChanged EntItemSelectionChanged;

        CtlExpanderItem objExpanderItem = null;
        object currentSelectedItem;
        public CtlExpanderContent()
        {
            try
            {
                this.InitializeComponent();





                //trvExpanderContent.ContextMenu = contextMenu1;
                //trvExpanderContent.ContextMenu.Visibility = Visibility.Hidden;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlExpanderContent()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void menuItem1_Click(object sender, RoutedEventArgs e)
        {
            VMuktiAPI.VMuktiHelper.CallEvent("RemoveBuddy", ((MenuItem)sender).Tag, null);
        }

        public void AddItem(string Caption)
        {
            try
            {
                objExpanderItem = new CtlExpanderItem();
                objExpanderItem.HorizontalAlignment = HorizontalAlignment.Stretch;
                objExpanderItem.VerticalAlignment = VerticalAlignment.Stretch;

                objExpanderItem.Image = "";
                objExpanderItem.Caption = Caption;

                TreeViewItem objTrviExpanderItem = new TreeViewItem();
                objTrviExpanderItem.HorizontalAlignment = HorizontalAlignment.Stretch;
                objTrviExpanderItem.VerticalAlignment = VerticalAlignment.Stretch;

                objTrviExpanderItem.GotFocus += new RoutedEventHandler(objTrviExpanderItem_GotFocus);
                objTrviExpanderItem.LostFocus += new RoutedEventHandler(objTrviExpanderItem_LostFocus);
                objTrviExpanderItem.Header = objExpanderItem;

                trvExpanderContent.Items.Add(objTrviExpanderItem);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddItem()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void AddItem(string Caption, bool ShowImage, ImageType objImageType, string strTag)
        {
            try
            {
                objExpanderItem = new CtlExpanderItem();


                objExpanderItem.HorizontalAlignment = HorizontalAlignment.Stretch;
                objExpanderItem.VerticalAlignment = VerticalAlignment.Stretch;
                if (objImageType == ImageType.Module || objImageType == ImageType.MaleBuddy || objImageType == ImageType.FemaleBuddy)
                {
                    objExpanderItem.AllowDrop = true;
                }

                objExpanderItem.Tag = strTag;
                //if (strTag == "offline")
                //{
                //   objExpanderItem.IsEnabled = false;
                //}

                objExpanderItem.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(objExpanderItem_PreviewMouseDown);
                objExpanderItem.MouseEnter += new System.Windows.Input.MouseEventHandler(objExpanderItem_MouseEnter);
                objExpanderItem.MouseLeave += new System.Windows.Input.MouseEventHandler(objExpanderItem_MouseLeave);
                if (ShowImage)
                {
                    if (objImageType == ImageType.Page)
                    {
                        objExpanderItem.Image = @"\Images\Page.png";
                    }
                    else if (objImageType == ImageType.Module)
                    {
                        objExpanderItem.Image = @"\Images\Modules.png";
                    }
                    else if (objImageType == ImageType.MaleBuddy)
                    {
                        objExpanderItem.Image = @"\Images\MBuddy.Png";
                    }
                    else if (objImageType == ImageType.FemaleBuddy)
                    {
                        objExpanderItem.Image = @"\Images\FBuddy.Png";
                    }
                }
                else
                {
                    objExpanderItem.Image = "";
                }

                objExpanderItem.Caption = Caption;

                if (strTag == "offline")
                {
                    AddPopUpMenu(objExpanderItem);
                    objExpanderItem.Image = @"\Images\FBuddy.Png";
                    objExpanderItem.ToolTip = Caption + " is offline";

                    objExpanderItem.PreviewMouseDown -= new System.Windows.Input.MouseButtonEventHandler(objExpanderItem_PreviewMouseDown);
                }
                else if (strTag == "online")
                {
                    AddPopUpMenu(objExpanderItem);
                    objExpanderItem.Image = @"\Images\MBuddy.Png";
                    objExpanderItem.ToolTip = Caption + " is online";

                    //objExpanderItem.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(objExpanderItem_PreviewMouseDown);
                }



                TreeViewItem objTrviExpanderItem = new TreeViewItem();
                objTrviExpanderItem.HorizontalAlignment = HorizontalAlignment.Stretch;
                objTrviExpanderItem.VerticalAlignment = VerticalAlignment.Stretch;

                objTrviExpanderItem.GotFocus += new RoutedEventHandler(objTrviExpanderItem_GotFocus);
                objTrviExpanderItem.LostFocus += new RoutedEventHandler(objTrviExpanderItem_LostFocus);
                objTrviExpanderItem.Header = objExpanderItem;


                trvExpanderContent.Items.Add(objTrviExpanderItem);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddItem()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void AddPopUpMenu(CtlExpanderItem ctlItem)
        {
            try
            {
                System.Windows.Controls.ContextMenu contextMenu1;
                contextMenu1 = new System.Windows.Controls.ContextMenu();

                System.Windows.Controls.MenuItem menuItem1;
                menuItem1 = new System.Windows.Controls.MenuItem();
                menuItem1.Header = "Remove Buddy";
                menuItem1.Tag = ctlItem.Caption;
                menuItem1.Click += new RoutedEventHandler(menuItem1_Click);
                contextMenu1.Items.Add(menuItem1);
                ctlItem.ContextMenu = contextMenu1;
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddPopUpMenu()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }

        }

        void objExpanderItem_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                ((CtlExpanderItem)sender).Background = System.Windows.Media.Brushes.Transparent;

                //if (((CtlExpanderItem)sender).Tag != null && (((CtlExpanderItem)sender).Tag.ToString() == "offline" || ((CtlExpanderItem)sender).Tag.ToString() == "online"))
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Visible;
                //    currentSelectedItem = ((CtlExpanderItem)sender).Caption;
                //}
                //else
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Hidden;
                //    currentSelectedItem = null;
                //}
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objExpanderItem_MouseLeave()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objExpanderItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                //((CtlExpanderItem)sender).Background = System.Windows.Media.Brushes.LightGray;
                //if (((CtlExpanderItem)sender).Tag != null && (((CtlExpanderItem)sender).Tag.ToString() == "offline" || ((CtlExpanderItem)sender).Tag.ToString() == "online"))
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Visible;
                //    currentSelectedItem = ((CtlExpanderItem)sender).Caption;
                //}
                //else
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Hidden;
                //    currentSelectedItem = null;
                //}
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objExpanderItem_MouseEnter()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void IsItemAdded(string Caption, string Status)
        {
            try
            {
                int j = 0;
                for (j = 0; j < trvExpanderContent.Items.Count; j++)
                {
                    if (((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Caption == Caption)
                    {
                        if (Status != ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Tag.ToString())
                        {
                            if (Status == "offline")
                            {

                                //t.Content
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Image = @"\Images\FBuddy.Png";
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).ToolTip = Caption + " is offline";
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Tag = Status;
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).PreviewMouseDown -= new System.Windows.Input.MouseButtonEventHandler(objExpanderItem_PreviewMouseDown);
                            }
                            else
                            {
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Image = @"\Images\MBuddy.Png";
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).ToolTip = Caption + " is online";
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).Tag = Status;
                                ((CtlExpanderItem)((TreeViewItem)trvExpanderContent.Items[j]).Header).PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(objExpanderItem_PreviewMouseDown);
                            }
                        }
                        break;
                    }
                }
                if (j == trvExpanderContent.Items.Count)
                {
                    AddItem(Caption, true, ImageType.MaleBuddy, Status);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "IsItemAdded()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objExpanderItem_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                if (objExpanderItem.AllowDrop == true)
                {
                    System.Windows.DragDrop.DoDragDrop((DependencyObject)((CtlExpanderItem)sender), ((CtlExpanderItem)sender), DragDropEffects.All);
                }
                else
                {
                    if (EntItemSelectionChanged != null)
                    {
                        EntItemSelectionChanged(((CtlExpanderItem)sender).Tag.ToString(), ((CtlExpanderItem)sender).Caption);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objExpanderItem_PreviewMouseDown()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objTrviExpanderItem_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                //if (((CtlExpanderItem)((TreeViewItem)sender).Header).Tag != null && (((CtlExpanderItem)((TreeViewItem)sender).Header).Tag.ToString() == "offline" || ((CtlExpanderItem)((TreeViewItem)sender).Header).Tag.ToString() == "online"))
                //{
                //    currentSelectedItem = ((CtlExpanderItem)((TreeViewItem)sender).Header).Caption;
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Visible;
                //}
                //else
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Hidden;
                //    currentSelectedItem = null;
                //}
                ((CtlExpanderItem)((TreeViewItem)sender).Header).GotFocus();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objTrviExpanderItem_GotFocus()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objTrviExpanderItem_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                ((CtlExpanderItem)((TreeViewItem)sender).Header).LostFocus();
                //if (((CtlExpanderItem)((TreeViewItem)sender).Header).Tag != null && (((CtlExpanderItem)((TreeViewItem)sender).Header).Tag.ToString() == "offline" || ((CtlExpanderItem)((TreeViewItem)sender).Header).Tag.ToString() == "online"))
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Visible;
                //}
                //else
                //{
                //    trvExpanderContent.ContextMenu.Visibility = Visibility.Hidden;
                //}

            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objTrviExpanderItem_LostFocus()--:--CtlExpanderContent.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        ~CtlExpanderContent()
        {
            Dispose();
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (EntItemSelectionChanged != null)
            {
                EntItemSelectionChanged = null;
            }
            if (objExpanderItem != null)
            {
                objExpanderItem = null;
            }
        }

        #endregion
    }
}
